﻿package com.yy.manager {
    import com.yy.base.App;
    
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class DragManager {

        private var _stage:Stage;
        private var _parent:DisplayObjectContainer;
        private var _update:Boolean = false;
        private var _offsetDic:Dictionary;
        private var _currentDragObject:DisplayObject;

        public function DragManager(){
            this._stage = App.appStage;
            this._stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseHandler);
            this._offsetDic = new Dictionary();
        }
        public function get parent():DisplayObjectContainer{
            return (((this._parent) || (this._stage)));
        }
        public function set parent(_arg1:DisplayObjectContainer):void{
            this._parent = _arg1;
        }
        public function get update():Boolean{
            return (this._update);
        }
        public function set update(_arg1:Boolean):void{
            this._update = _arg1;
        }
        private function mouseHandler(_arg1:MouseEvent=null):void{
            var _local2:Object;
            var _local3:Point;
            for (_local2 in this._offsetDic) {
                if (this._offsetDic[_local2] == null){
                    _local2.x = this._stage.mouseX;
                    _local2.y = this._stage.mouseY;
                } else {
                    _local3 = (this._offsetDic[_local2] as Point);
                    _local2.x = (this._stage.mouseX - _local3.x);
                    _local2.y = (this._stage.mouseY - _local3.y);
                };
            };
            if (((this.update) && (_arg1))){
                _arg1.updateAfterEvent();
            };
        }
        public function startDrag(_arg1:DisplayObject, _arg2:Boolean=false, _arg3:Point=null, _arg4:DisplayObjectContainer=null):void{
            var _local5:Point;
            if (_arg2){
                _local5 = new Point((_arg1.width / 2), (_arg1.height / 2));
                if (_arg3 == null){
                    _arg3 = _local5;
                } else {
                    _arg3.offset(_local5.x, _local5.y);
                };
            };
            if ((_arg1 is DisplayObjectContainer)){
                (_arg1 as DisplayObjectContainer).mouseChildren = false;
            };
            if ((_arg1 is InteractiveObject)){
                (_arg1 as InteractiveObject).mouseEnabled = false;
            };
            this._currentDragObject = _arg1;
            this._offsetDic[_arg1] = _arg3;
            if (_arg4 == null){
                this.parent.addChildAt(_arg1, 0);
            } else {
                _arg4.addChildAt(_arg1, 0);
            };
            this.mouseHandler();
        }
        public function stopDrag(_arg1:DisplayObject):void{
            if (_arg1.parent){
                _arg1.parent.removeChild(_arg1);
            };
            delete this._offsetDic[_arg1];
        }
        public function getDragObject():DisplayObject{
            return (this._currentDragObject);
        }
        public function getDragObjectList():Array{
            var _local2:Object;
            var _local1:Array = [];
            for (_local2 in this._offsetDic) {
                _local1.push(_local2);
            };
            return (_local1);
        }
        public function stopAll():void{
            var _local1:Object;
            for (_local1 in this._offsetDic) {
                this.stopDrag((_local1 as DisplayObject));
            };
        }

    }
}